const MapCtrl = {
    initMap() {
        minemap.domainUrl = '//minedata.cn';
        minemap.spriteUrl = '//minedata.cn/minemapapi/v1.3/sprite/sprite';
        minemap.serviceUrl = '//minedata.cn/service';
        minemap.accessToken = 'a3622f2702c34957a160242d2eb0e508';
        minemap.solution = 3756;
        minemap.TRW = {
            stayStatic: true
        };

        let map = new minemap.Map({
            container: 'map',
            style: "//minedata.cn/service/solu/style/id/3756",
            center: [108.952, 34.223],
            zoom: 11,
            pitch: 0,
            maxZoom: 14.9,
            minZoom: 10,
            logoControl: false
        });
        map.repaint = true;

        return map;
    },

    initWeatherSource(map) {
        // map.addSource("weather", {
        //     "type": "vector",
        //     'tiles': ['//dev.minedata.cn/dataweather/roadweather/{z}/{x}/{y}']
        // });

        // map.addSource("weather-pre", {
        //     "type": "vector",
        //     'tiles': ['//dev.minedata.cn/dataweather/roadregionweather/{z}/{x}/{y}']
        // });
        map.addSource('HistXian', {
            "type": "vector",
            "separate": {
                "isRitcSeparate": true,
                "hasTimeStamp": true,
                "separateSource": "HistXian",
                "separateLayer": "HistXian",
                "valueUrl": "//dev.minedata.cn/data/rtichist/HistXian/{z}_{x}_{y}_{t}?token=ca78282dfff148e3be5390ce027068f5&solu=3456",
            },
            "tiles": ["//dev.minedata.cn/data/rtichist/HistXian/{z}/{x}/{y}?token=ca78282dfff148e3be5390ce027068f5&solu=3456"]
        });

        map.addSource("weather-pre", {
            "type": "geojson",
            'data': '../data/pre_regiion_n_change.json'
        });

        map.addSource("weather-vism", {
            "type": "vector",
            'tiles': ['//dev.minedata.cn/dataweather/roadvismweather/{z}/{x}/{y}']
        });

        map.addSource("road-speed", {
            "type": "geojson",
            'data': '../data/roadSpeed.json'
        });

        map.addSource("od", {
            "type": "vector",
            'tiles': ['//dev.minedata.cn/data/general3d/ODXian/{z}/{x}/{y}?token=789cc01d1b464d05ba4badf8c6082679&solu=1590']
        });

        MapCtrl._initWeatherTemSource(map);
    },

    _initWeatherTemSource(map) {
        for(let i = 0; i < 25; i++) {
            map.addSource("tem" + i, {
                "type": "image",
                "url": "../img/tem-png/" + i + "00.png",
                "coordinates": [
                    [108, 35.075],
                    [110.525, 35.075],
                    [110.525, 33.45],
                    [108, 33.45]
                ]
            });
        }
    },

    initWeatherLayers(map, backgroundId, pid) {

        MapCtrl._initWeatherTemLayers(map, backgroundId, pid, 0.5);
        MapCtrl._initWeatherVismLayers(map, backgroundId, pid);
        MapCtrl._initRoadCondLayers(map, backgroundId, pid);
        MapCtrl._initOdLayers(map, backgroundId, pid);
        MapCtrl._initPreLayers(map, backgroundId, pid);

        map.loadImage('../img/ic_details1@2x.png', function (error, image) {
            if (error) throw error;
            map.addImage('ic_details', image);
            map.addLayer({
                "id": 'road-speed-symbol',
                "source": 'road-speed',
                "type": "symbol",
                "layout": {
                    "icon-image": "ic_details",
                    "icon-size": 0.5
                },
                "paint": {}
            })
        });
    },

    _initOdLayers(map, backgroundId, pid) {
        map.addLayer({
            "id": "od-road",
            "type": "line",
            "source": "od",
            "source-layer": "ODXian",
            "layout": {
                "line-cap": "round",
                "line-join": "round",
                "visibility": "none",
            },
            "paint": {
                "line-color": "#3F4BF8", //32CD32 CD8500
                "line-width": 0.7,
                "line-opacity": 0.3
            },
            "minzoom": 3,
            "maxzoom": 18,
            "filter": ["==", "zone_id", 0]
        });

        map.addLayer({
            "id": "od-tracking-in",
            "type": "tracking",
            "source": "od",
            "source-layer": "ODXian",
            "layout": {
                "tracking-join": "round",
                "tracking-cap": "round",
                "visibility": "none",
            },
            "paint": {
                'tracking-type': 'history-manual',
                'tracking-seg-count': 550,
                'tracking-speed': 20,
                "tracking-color": "#B6F6FF",//fff244  ff4e00  e91818  AC6E13
                "tracking-width": 2,
                'tracking-opacity': 1,
                'tracking-seg-group': 10
            },
            "minzoom": 3,
            "maxzoom": 18,
            "filter": ['all', ["==", "direct", 0], ["==", "zone_id", 0]]
        });

        map.addLayer({
            "id": "od-tracking-out",
            "type": "tracking",
            "source": "od",
            "source-layer": "ODXian",
            "layout": {
                "tracking-join": "round",
                "tracking-cap": "round",
                "visibility": "none",
            },
            "paint": {
                'tracking-type': 'history-manual',
                'tracking-seg-count': 550,
                'tracking-speed': 20,
                "tracking-color": "#FF89F2",//fff244  ff4e00  e91818  AC6E13
                "tracking-width": 2,
                'tracking-opacity': 1,
                'tracking-seg-group': 10
            },
            "minzoom": 3,
            "maxzoom": 18,
            "filter": ['all', ["==", "direct", 1], ["==", "zone_id", 0]]
        });
    },

    _initPreLayers(map, backgroundId, pid) {
        map.addLayer({
            "id": "pre",
            "maxzoom": 19,
            "source": "weather-pre",
            // "source-layer": "RoadRegionWeather",
            "layout": {
                "visibility": "none",
                'histogram-color-render': true,
            },
            "paint": {
                'histogram-dynamic': true,
                // "histogram-color": "#0072E3",
                "histogram-base": 0,
                "histogram-opacity": 0.8,
                "histogram-height": {
                    "property": "precipitation0",
                    "type": "identity"
                },
                "histogram-heightArr1": {
                    "property": "precipitation1",
                    "type": "identity"
                },
                "histogram-heightArr2": {
                    "property": "precipitation2",
                    "type": "identity"
                },
                "histogram-heightArr3": {
                    "property": "precipitation3",
                    "type": "identity"
                },
                // "histogram-height": 0,
                // "histogram-heightArr1": 'rgba(20, 40, 60, 80)',
                // "histogram-heightArr2": 'rgba(100, 120, 130, 100)',
                // "histogram-heightArr3": 'rgba(80, 60, 40, 20)',
                "histogram-colors": ['#0049C2', '#0049C2', '#0B98E3', '#5BFDF8', '#C7F5FF'],
                'histogram-max-height': 400,
                'histogram-count': 12,
                "histogram-time": 2000
            },
            "minzoom": 1,
            "type": "histogram"
        });
    },

    _initRoadCondLayers(map, backgroundId, pid) {
        const roadCondLineWidths = [{"stops": [[5, 1], [18, 4]], "base": 1.2}, {
            "stops": [[5, 1], [18, 3]],
            "base": 1.2
        }, {"stops": [[5, 1], [18, 2.5]], "base": 1.2}, {"stops": [[5, 1], [18, 2]], "base": 1.2}]
        const roadCondFilters = [["in", "function_class", 1, 2], ["==", "function_class", 3], ["==", "function_class", 4], ["==", "function_class", 5]];
        const roadCondMaxZooms = [17.5, 17.5, 17.5, 17.5];
        const roadCondMinZooms = [6.0, 11.0, 13.0, 14.0];

        for (let i = 0; i < 4; i++) {
            map.addLayer({
                "id": "road-cond-" + parseInt(i + 1),
                "type": "line",
                "source": "HistXian",
                "source-layer": "HistXian",
                "maxzoom": roadCondMaxZooms[i],
                "minzoom": roadCondMinZooms[i],
                "layout": {
                    "line-join": "round",
                    "line-cap": "round",
                    "visibility": "visible",
                },
                "paint": {
                    "line-color": {
                        "stops": [[0, "#999999"], [1, "#66cc00"], [2, "#ff9900"], [3, "#cc0000"]],
                        "property": "status"
                    },
                    "line-width": roadCondLineWidths[i]
                },
                "filter": roadCondFilters[i]
            }, '1565c97198b14d7b88ba3b1c96f99842');
        }
    },

    _initWeatherVismLayers(map, backgroundId, pid) {
        for (let i = 0; i < 24; i++) {
            map.addLayer({
                "id": "vism" + i,
                "type": "line",
                "source": "weather-vism",
                "source-layer": "RoadVismWeather",
                "layout": {
                    "line-join": "round",
                    "line-cap": "round",
                    'visibility': 'visible'
                },
                "paint": {
                    "line-color": {
                        "property": "vism" + i,
                        "stops": [
                            // [10, '#8E71FF'],
                            // [12, '#FE4B3C'],
                            // [15, '#E0B62C'],
                            // [18, '#28AD86']
                            [1, 'rgba(221, 221, 221, 0.95)'],
                            [4, 'rgba(221, 221, 221, 0.6)'],
                            [10, 'rgba(221, 221, 221, 0.3)'],
                            [20, 'rgba(221, 221, 221, 0.05)']
                        ]
                    },
                    "line-width": 3
                }
            }, backgroundId);
        }
    },

    _initWeatherTemLayers(map, backgroundId, pid, opacity) {
        for (let i = 0; i < 25; i++) {
            map.addLayer({
                "id": "tem" + i,
                "type": "raster",
                "source": "tem" + i,
                "minzoom": 3,
                "maxzoom": 17,
                "layout": {
                    "visibility": "none"
                },
                "paint": {
                    "raster-opacity": opacity
                }
            }, backgroundId);
        }
    }
};

export default MapCtrl;